package com.photography.wedding.controller;

import com.photography.wedding.pojo.Picture;
import com.photography.wedding.pojo.UploadFile;
import com.photography.wedding.util.DateUtil;
import com.photography.wedding.util.ReturnUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UploadController {

    @RequestMapping("/uploadFile")
    @ResponseBody
    public ModelMap uploadFile(@RequestParam("userId") String userId, @RequestParam("pictureUrl") MultipartFile file, HttpServletRequest request) {
        if(StringUtils.isEmpty(userId)){
            return ReturnUtil.error("用户ID不能为空", null, null);
        }

        String fileName = file.getOriginalFilename();
        if(fileName.indexOf("\\") != -1){
            fileName = fileName.substring(fileName.lastIndexOf("\\"));
        }

        fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".jpeg";

        String filePath = "src/main/webapp/weddingPhotography/files/images/" + userId + "/";
        File targetFile = new File(filePath);
        if(!targetFile.exists()){
            targetFile.mkdirs();
        }

        String urlPath = "/weddingPhotography/files/images/" + userId + "/" + fileName;

        UploadFile uploadFile = new UploadFile();
        uploadFile.setUserId(userId);
        uploadFile.setPictureTitle(fileName);
        uploadFile.setPictureUrl(urlPath);
        uploadFile.setCreateTime(DateUtil.getCurrentTime());

        System.out.println(uploadFile.toString());

        FileOutputStream out = null;
        try {
            out = new FileOutputStream(filePath + fileName);
            out.write(file.getBytes());
            out.flush();
            out.close();
            return ReturnUtil.success("添加图片成功", uploadFile);
        } catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }

    }

    @RequestMapping("/newsUploadFile")
    @ResponseBody
    public ModelMap newsUploadFile(@RequestParam("newsType") String newsType, @RequestParam("pictureUrl") MultipartFile file, HttpServletRequest request) {
        if(StringUtils.isEmpty(newsType)){
            return ReturnUtil.error("新闻类型不能为空", null, null);
        }

        String fileName = file.getOriginalFilename();
        if(fileName.indexOf("\\") != -1){
            fileName = fileName.substring(fileName.lastIndexOf("\\"));
        }

        fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".jpeg";

        String filePath = "src/main/webapp/weddingPhotography/files/" + newsType + "/";
        File targetFile = new File(filePath);
        if(!targetFile.exists()){
            targetFile.mkdirs();
        }

        String urlPath = "/weddingPhotography/files/" + newsType + "/" + fileName;

        UploadFile uploadFile = new UploadFile();
        uploadFile.setPictureTitle(fileName);
        uploadFile.setPictureUrl(urlPath);
        uploadFile.setCreateTime(DateUtil.getCurrentTime());

        System.out.println(uploadFile.toString());

        FileOutputStream out = null;
        try {
            out = new FileOutputStream(filePath + fileName);
            out.write(file.getBytes());
            out.flush();
            out.close();
            return ReturnUtil.success("添加图片成功", uploadFile);
        } catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }

    }

}
